package com.ncmmall.api.apis.mall;

import com.feijin.commons.lang.ArithUtils;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.domain.business.goods.sku.GoodsSku;
import com.ncmmall.domain.business.marketing.coupon.Coupon;
import com.ncmmall.domain.business.marketing.coupon.CouponMemberDraw;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

public class UserCoupon {

    /**
     * 可领取
     */
    @Getter
    private final List<Coupon> notDraw;

    /**
     * 已领取
     */
    @Getter
    private final List<Coupon> draw;

    /**
     * 可使用
     */
    @Getter
    private final List<Coupon> usableDraw;

    public UserCoupon(Goods goods, WebUser user, QueryChannelService queryChannelService) {

        //查询适用于此商品的优惠券
        List<Coupon> all = queryChannelService.findAll("from Coupon c where c.merchant.id = :mid and c.scopeType = 1",
                ImmutableMap.of("mid", goods.getMerchant().getId()), Coupon.class);
        List<Coupon> coupons = queryChannelService.findAll("select cg.coupon from CouponGoods cg where cg.goods.id = :gid",
                ImmutableMap.of("gid", goods.getId()), Coupon.class);
        all.addAll(coupons);

        notDraw = new ArrayList<>();
        draw = new ArrayList<>();
        usableDraw = new ArrayList<>();

        if (user != null) {
            for (Coupon coupon : all) {
                CouponMemberDraw memberDraw = queryChannelService.queryOneEntity("select * from mall_coupon_member_draw where coupon_id = :cid and member_id = :mid limit 1", ImmutableMap.of("cid", coupon.getId(), "mid", user.getId()), CouponMemberDraw.class);
                if (memberDraw != null) {
                    coupon.setGetStatus(memberDraw.getStatus());
                    coupon.setExpirationTime(memberDraw.getExpirationTime());
                    coupon.setDrawTime(memberDraw.getDrawTime());
                    draw.add(coupon);
                    if (memberDraw.getStatus() == 1) {
                        usableDraw.add(coupon);
                    }
                } else {
                    if (coupon.getPublishStatus() == 2 && coupon.getStatus() != 5) {
                        notDraw.add(coupon);
                    }
                }
            }
        } else {
            notDraw.addAll(all);
        }
    }


    public UserCoupon(GoodsSku sku, int quantity, WebUser user, QueryChannelService queryChannelService) {

        Goods goods = sku.getGoods();

        //查询适用于此商品的优惠券
        List<Coupon> all = queryChannelService.findAll("from Coupon c where c.merchant.id = :mid and c.scopeType = 1",
                ImmutableMap.of("mid", goods.getMerchant().getId()), Coupon.class);
        List<Coupon> coupons = queryChannelService.findAll("select cg.coupon from CouponGoods cg where cg.goods.id = :gid",
                ImmutableMap.of("gid", goods.getId()), Coupon.class);
        all.addAll(coupons);

        double price = ArithUtils.roundHalfUp(sku.getPrice() * quantity);
        all.removeIf(coupon -> coupon.getConditionAmount() > price);

        notDraw = new ArrayList<>();
        draw = new ArrayList<>();
        usableDraw = new ArrayList<>();

        if (user != null) {
            for (Coupon coupon : all) {
                CouponMemberDraw memberDraw = queryChannelService.queryOneEntity("select * from mall_coupon_member_draw where coupon_id = :cid and member_id = :mid limit 1", ImmutableMap.of("cid", coupon.getId(), "mid", user.getId()), CouponMemberDraw.class);
                if (memberDraw != null) {
                    coupon.setGetStatus(memberDraw.getStatus());
                    coupon.setExpirationTime(memberDraw.getExpirationTime());
                    coupon.setDrawTime(memberDraw.getDrawTime());
                    draw.add(coupon);
                    if (memberDraw.getStatus() == 1) {
                        usableDraw.add(coupon);
                    }
                } else {
                    if (coupon.getPublishStatus() == 2 && coupon.getStatus() != 5) {
                        notDraw.add(coupon);
                    }
                }
            }
        } else {
            notDraw.addAll(all);
        }
    }


    /**
     * @param merchant 商家
     * @param user     买家
     */
    public UserCoupon(WebUser merchant, WebUser user, QueryChannelService queryChannelService) {

        Requires.notNull(user, "用户不存在");

        List<Coupon> all = queryChannelService.findAll("from Coupon c where c.publishStatus = 2 and c.merchant.id = :mid and c.scopeType = 1",
                ImmutableMap.of("mid", merchant.getId()), Coupon.class);

        notDraw = new ArrayList<>();
        draw = new ArrayList<>();
        usableDraw = new ArrayList<>();

        for (Coupon coupon : all) {
            CouponMemberDraw memberDraw = queryChannelService.queryOneEntity("select * from mall_coupon_member_draw where coupon_id = :cid and member_id = :mid limit 1", ImmutableMap.of("cid", coupon.getId(), "mid", user.getId()), CouponMemberDraw.class);
            if (memberDraw != null) {
                coupon.setGetStatus(memberDraw.getStatus());
                coupon.setExpirationTime(memberDraw.getExpirationTime());
                coupon.setDrawTime(memberDraw.getDrawTime());
                draw.add(coupon);

                if (memberDraw.getStatus() == 1) {
                    usableDraw.add(coupon);
                }
            } else {
                notDraw.add(coupon);
            }
        }
    }
}